//
//  Utility.h

#import <Cocoa/Cocoa.h>
#import <QuickTime/QuickTime.h>


/* Set a passed in BOOL to its value in the preferences; if it doesn't exist,
 * the BOOL isn't changed.
 */
void getBoolFromPreferences(NSUserDefaults *userPrefs, NSString* key, BOOL *pValue);
int getIntFromPreferences(NSUserDefaults *userPrefs, NSString *key, int defValue);
NSString* getStringFromPreferences(NSUserDefaults *userPrefs, NSString *key, NSString *defValue);
NSColor* getColorFromPreferences(NSUserDefaults *userPrefs, NSString *key, NSColor *defValue);

void putColorInPreferences(NSUserDefaults* userPrefs, NSString *key, NSColor *value);


/*
 * Return a string containing the pictures folder for the current user
 */
NSString* GetPicturesFolder();


/*
 * Standard compare functions that take ThumbData - and sorts by name, date or (media) type.
 */
int compareThumbDataNames(id obj1, id obj2, void *context);
int compareThumbDataDates(id obj1, id obj2, void *context);
int compareThumbDataTypes(id obj1, id obj2, void *context);
int compareThumbStates(id obj1, id obj2, void *context);

/*
 * Standard compare functions that take NSString - and sorts by name or date.
 * If context is non-nil, it's assumed to be the path for the provided NSString
 * (filenames) when using compareFileDates.
 */
int comparePaths(id obj1, id obj2, void *context);
int compareFileDates(id obj1, id obj2, void *context);

/*
 * Returns the exif date, if it exists, or the last modified date if not.
 */
NSDate *exifOrFileDate(NSString *filename);

/*
 * Returns the length formatted: 32 KB, 66 MB, 1 GB
 */
NSString* formatLength(UInt64 length);

/*
 * Generate and return a thumbnail scaled to the given width and height. Only
 * works with JPEGs.
 */
NSData* generateThumbData(NSString *filename, int maxWidth, int maxHeight);
NSImage* generateThumbnail(NSString *filename, int maxWidth, int maxHeight);


/*
 * Retrieves EXIF data - if pList is non-NULL, it'll be filled with ExifItems for everything in
 * the EXIF structures (which does not include the thumbnail). If pDate is non-NULL, the original
 * date will be returned. And if pThumbnail is non-NULL, the actual thumbnail will be returned.
 * If there isn't any EXIF data, or if the requested items (date, thumbnail) don't exist, the
 * values won't be set to anything.
 */
void getExifData(NSString *filename, NSMutableArray **pList, NSDate **pDate, NSImage **pThumbnail);


/*
 * Change all the EXIF dates in the given file by the specified amount. YES is returned if everything
 * went well. NO is returned if something didn't work. (YES is returned if there is either no EXIF
 * data, or there is, but there are no dates).
 * The new date is returned in pNewDate
 */
BOOL setExifDates(NSString *filename, long delta, NSDate **pNewDate);

/*
 * Generate a thumbnail (by calling the embedded 'exiftran' executable
 */
int generateExifThumbnail(NSString *filename);

/*
 * Rotate methods (which just call into an embedded 'exiftran' executable
 */
int rotate90(NSString *filename);
int rotate270(NSString *filename);


// Private
NSImage*  imageFromMovieFile(NSString* filename);
NSImage*  imageFromMovie(Movie movie, TimeValue time);
NSImage*  imageFromGWorld(GWorldPtr gWorldPtr);
GWorldPtr gworldForMovie(Movie movie);


void linearShadedColor(void *info, const float *in, float *out);
void linearGradientFill(NSColor* firstColor, NSColor* secondColor, CGRect rect);
void gradientFill(
		CGFunctionCallbacks callbacks,
		NSColor* firstColor,
		NSColor* secondColor,
		CGRect rect);
